/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package movierental.service.impl;
import org.springframework.stereotype.Service;
import java.util.List;
import movierental.entity.Movie;
import movierental.entity.PenyewaanHeader;
import movierental.service.AppService;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author abet
 */
@Service("appService")
@Transactional(readOnly=true)
public class AppServiceImpl implements AppService {
    @Autowired 
    private SessionFactory sessionFactory;

    @Override
    @Transactional(readOnly=false)
    public void saveMovie(Movie movie) {
        sessionFactory.getCurrentSession().saveOrUpdate(movie);
    }
    @Override
    @Transactional(readOnly=false)
    public void deleteMovie(Movie movie) {
        sessionFactory.getCurrentSession().delete(movie);
    }

    @Override
    public List<Movie> findAllMovie() {
        return (List<Movie>) sessionFactory.getCurrentSession()
		.createQuery("from Movie").list();
    }

    @Override
    public Movie findMovieByKode(String kode) {
        if (kode==null) return null;
		return (Movie) sessionFactory.getCurrentSession()
		.createQuery("FROM Movie m WHERE m.kodeMovie=:kd")
		.setParameter("kd", kode)
		.uniqueResult();
    }

    @Override
    public Movie findMovieByJudul(String judul) {
        if (judul==null) return null;
		return (Movie) sessionFactory.getCurrentSession()
		.createQuery("FROM Movie m WHERE m.judulMovie=:jd")
		.setParameter("jd",judul)
		.uniqueResult();
    }
    @Override
    @Transactional(readOnly=false)
    public void saveTransaction(PenyewaanHeader sewaHeader) {
        sessionFactory.getCurrentSession().saveOrUpdate(sewaHeader);
    }
    
}
